package com.yootoo.bsuser.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;

@Configuration
public class RabbitConfig {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Value("${spring.rabbitmq.host}")
	private String host;

	@Value("${spring.rabbitmq.port}")
	private int port;

	@Value("${spring.rabbitmq.username}")
	private String username;

	@Value("${spring.rabbitmq.password}")
	private String password;
	
	@Bean
	public ConnectionFactory connectionFactory() {
		CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
		connectionFactory.setUsername(username);
		connectionFactory.setPassword(password);
		connectionFactory.setVirtualHost("/");
		connectionFactory.setPublisherConfirms(true);
		return connectionFactory;
	}

//	public static final String EXCHANGE_A = "my-mq-exchange_A";
//	public static final String EXCHANGE_B = "my-mq-exchange_B";
//	public static final String EXCHANGE_C = "my-mq-exchange_C";
//
//	public static final String QUEUE_A = "QUEUE_A";
//	public static final String QUEUE_B = "QUEUE_B";
//	public static final String QUEUE_C = "QUEUE_C";
//
//	public static final String ROUTINGKEY_A = "spring-boot-routingKey_A";
//	public static final String ROUTINGKEY_B = "spring-boot-routingKey_B";
//	public static final String ROUTINGKEY_C = "spring-boot-routingKey_C";
	/**
//	 * 针对消费者配置 1. 设置交换机类型 2. 将队列绑定到交换机 FanoutExchange: 将消息分发到所有的绑定队列，无routingkey的概念
//	 * HeadersExchange ：通过添加属性key-value匹配 DirectExchange:按照routingkey分发到指定队列
//	 * TopicExchange:多关键字匹配
//	 */
//	@Bean
//	public DirectExchange defaultExchange() {
//		return new DirectExchange(EXCHANGE_A);
//	}
//
//	/**
//	 * 获取队列A
//	 * 
//	 * @return
//	 */
//	@Bean
//	public Queue queueB() {
//		return new Queue(QUEUE_B, true); // 队列持久
//	}
//
//	/**
//	 * 获取队列A
//	 * 
//	 * @return
//	 */
//	@Bean
//	public Queue queueA() {
//		return new Queue(QUEUE_A, true); // 队列持久
//	}
//
//	@Bean
//	public Binding binding() {
//		return BindingBuilder.bind(queueA()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_A);
//	}
//	
//
//    @Bean 
//    public Binding bindingB(){ 
//		 return BindingBuilder.bind(queueB()).to(defaultExchange()).with(RabbitConfig. ROUTINGKEY_B);
//	}
//
//
//	final static String message = "topic.message";
//	final static String messages = "topic.messages";
//
//	@Bean
//	public Queue Queue() {
//		return new Queue("registerUserQueue");
//	}
//
//	@Bean
//	public Queue QueueforObject() {
//		return new Queue("objectQueue");
//	}
//
//	@Bean
//	public Queue queueMessage() {
//		return new Queue("topic.message");
//	}
//
//	@Bean
//	public Queue queueMessages() {
//		return new Queue("topic.messages");
//	}
//
//	@Bean
//	TopicExchange exchange() {
//		return new TopicExchange("exchange");
//	}
//
//	@Bean
//	Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
//		return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
//	}
//
//	@Bean
//	Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
//		return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
//	}	

	/**
	 * 订单状态变化
	 */
	public static final String EXCHANGE_ORDERFORSTS_CHANGE = "orderForStsChange";
	/**
	 * 贷款状态变化
	 */
	public static final String ROUTINGKEY_LOAN_ORDERFORSTS_OPERATE = "loan.order.item.sts.change.operate";
	/**
	 * 订单状态变化交换机
	 * @return
	 */
	@Bean
	public TopicExchange orderForStsChange() {
		return new TopicExchange("orderForStsChange");
	}
	
	/**
	 * 贷款订单状态改变
	 * @return
	 */
	@Bean
	public Queue loanOrderStsQueue() {
		return new Queue("loan.order.sts.item.change.operate");
	}
	/**
	 * 贷款订单状态变化绑定器
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingLoanOrderStsExchangeMessage() {
		return BindingBuilder.bind(loanOrderStsQueue()).to(orderForStsChange()).with(ROUTINGKEY_LOAN_ORDERFORSTS_OPERATE);
	}
	
	public static final String ROUTINGKEY_INS_ORDERFORSTS_OPERATE = "ins.order.item.sts.change.operate";
	
	/**
	 * 保险订单状态改变
	 * @return
	 */
	@Bean
	public Queue insOrderStsQueue() {
		return new Queue("ins.order.sts.item.change.operate");
	}
	/**
	 * 贷款订单状态变化绑定器
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingInsOrderStsExchangeMessage() {
		return BindingBuilder.bind(insOrderStsQueue()).to(orderForStsChange()).with(ROUTINGKEY_INS_ORDERFORSTS_OPERATE);
	}
	public static final String ROUTINGKEY_ORDER_FORSTS_EMAIL = "*.order.item.sts.change.email";
	/**
	 * 订单状态发送邮件
	 * @return
	 */
	@Bean
	public Queue emailOrderStsQueue() {
		return new Queue("*.order.sts.item.change.email");
	}
	/**
	 * 贷款订单状态变化绑定器
	 * @param queueMessage
	 * @param exchange
	 * @return
	 */
	@Bean
	Binding bindingEmailOrderStsExchangeMessage() {
		return BindingBuilder.bind(emailOrderStsQueue()).to(orderForStsChange()).with(ROUTINGKEY_ORDER_FORSTS_EMAIL);
	}
}